package com.lapelittle.framework.task;

import com.lapelittle.framework.service.EmailService;
import com.lapelittle.framework.service.TokenService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@EnableScheduling
public class CleanupTask {
    private static final Logger logger = LoggerFactory.getLogger(CleanupTask.class);
    
    @Autowired
    private TokenService tokenService;
    
    @Autowired
    private EmailService emailService;
    
    /**
     * 每5分钟执行一次清理过期验证码
     */
    @Scheduled(fixedRate = 300000)
    public void cleanExpiredCodes() {
        try {
            int count = emailService.cleanExpiredCodes();
            logger.info("清理过期验证码完成，共清理{}个", count);
        } catch (Exception e) {
            logger.error("清理过期验证码失败", e);
        }
    }
    
    /**
     * 每小时执行一次清理过期token
     */
    @Scheduled(fixedRate = 3600000)
    public void cleanExpiredTokens() {
        try {
            int count = tokenService.cleanExpiredTokens();
            logger.info("清理过期token完成，共清理{}个", count);
        } catch (Exception e) {
            logger.error("清理过期token失败", e);
        }
    }
} 